﻿using System.Net.Mail;
using System.Net;
using System.Text;
using System.Threading;
using System.Collections.Generic;

namespace Codeplus.Library.Utils
{
    public class Mail
    {
        private const string GoogleSmtpServer = "smtp.gmail.com";
        private const int GoogleSmtpPort = 587;
        private const string AccountUsername = "admin@codeplus.in";
        //keeping passwords in code is more secure than in web.config in a shared hosting environment
        private const string AccountPassword = "London@99";

        public const string SenderAccount = "admin@codeplus.in";
        public const string Opportunity = "opportunity@codeplus.in";
        public const string Support = "support@codeplus.in";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="from"></param>
        /// <param name="recipients"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        public void Send(string from, string recipients, string subject, string body)
        {
            this.Send(from, recipients, subject, body, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="from"></param>
        /// <param name="recipients"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="async"></param>
        public void Send(string from, string recipients, string subject, string body, bool async)
        {
            var mailer = new SmtpClient(GoogleSmtpServer, GoogleSmtpPort);
            mailer.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailer.UseDefaultCredentials = false;
            mailer.Credentials = new NetworkCredential(AccountUsername, AccountPassword);
            mailer.EnableSsl = true;

            var message = new MailMessage(from, recipients, subject, body);
            message.BodyEncoding = Encoding.UTF8;
            message.IsBodyHtml = true;
            if (async)
            {
                var threadArgs = new List<object>()
                                     {
                                         mailer,
                                         message
                                     };
                ThreadPool.QueueUserWorkItem(SendMailWorkItem, threadArgs);
            }
            else
            {
                using (mailer)
                {
                    mailer.Send(message);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="threadArgs"></param>
        private void SendMailWorkItem(object threadArgs)
        {
            var args = threadArgs as List<object>;
            if (args == null) return;

            var mailer = args[0] as SmtpClient;
            var message = args[1] as MailMessage;

            if (mailer == null) return;
            if (message == null) return;

            using (mailer)
            {
                mailer.Send(message);
            }
        }
    }
}
